﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Security;

namespace FormProj
{
    public partial class Login : System.Web.UI.Page
    {
        //http://www.cnblogs.com/luomingchao/articles/474674.html
        //http://blog.csdn.net/kenshincui/article/details/5559508
        protected void Page_Load(object sender, EventArgs e)
        {
            string userName = User.Identity.IsAuthenticated.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if(this.txtUserName.Text=="admin"&&this.txtPwd.Text=="1")
            {
                FormsAuthentication.RedirectFromLoginPage(this.txtUserName.Text,true);
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(this.txtUserName.Text,true,30000);
                string authTicket = FormsAuthentication.Encrypt(ticket);
                HttpCookie coo = new HttpCookie(FormsAuthentication.FormsCookieName,authTicket);
                Response.Cookies.Add(coo);

                var userData = ((FormsIdentity)this.Context.User.Identity).Ticket.UserData;
                //FormsAuthentication.SetAuthCookie(this.txtUserName.Text,true);

                if(this.Request.IsAuthenticated)
                {
                    if(userData!=this.Request.UserHostAddress)
                    {
                        System.Security.Principal.GenericIdentity gi = new System.Security.Principal.GenericIdentity("","");
                        string[] rolesi = { };
                        System.Security.Principal.GenericPrincipal gpi = new System.Security.Principal.GenericPrincipal(gi, rolesi);
                        this.Context.User = gpi;
                    }
                }
            }
        }
    }
}